package org.example.controller;

import org.example.annotation.LoginCheck;
import org.example.kafka.Event;
import org.example.kafka.MessageProducer;
import org.example.service.imp.LikeServiceImp;
import org.example.util.CommonParam;
import org.example.util.UserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/like/")
public class LikeController {
    @Autowired
    private MessageProducer messageProducer;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private LikeServiceImp likeServiceImp;
    @Autowired
    private UserThreadLocalUtil userThreadLocalUtil;

    /**
     * 有问题  哎 以后写功能写完 测试完在写下一个 好吗？  给二级评论点赞有问题  不写了  导致kafka通知消息出现问题  以后小心点  这个放了
     * @param type
     * @param id
     * @param likeUserId
     * @return
     */
    @LoginCheck
    @PostMapping("/like")
    @ResponseBody
    public Map<String,Integer> like(int type, int id,int likeUserId){
        Map<String,Integer> result = new HashMap<>();
        //当前用户是否点赞了
        int currId= userThreadLocalUtil.get().getId();
        boolean like = likeServiceImp.currUserIsLike(type, id, currId);

        if (like){
            //取消
            likeServiceImp.cancelLike(type,id,currId);
            result.put("likeStatus",0);
            result.put("likeCount",likeServiceImp.getLikeCount(type,id));
            System.out.println();
        }else {
            //点赞
            likeServiceImp.like(type,id,currId);
            result.put("likeStatus",1);
            result.put("likeCount",likeServiceImp.getLikeCount(type,id));
            System.out.println();
        }
        //因为主页展示点赞  需要从redis中获取ids  我们点赞需要将id存储进去
        String userKye = CommonParam.USER_PUBLISH_STATUS+userThreadLocalUtil.get().getId();
        try {
            if(type == 1){
                //将帖子 id 存储到热贴排行中
                likeServiceImp.saveDiscussIdToHotKey(id);
                //帖子
                if (redisTemplate.hasKey(userKye)){
                    Set<Integer> originSet = (Set<Integer>)redisTemplate.opsForHash().get(userKye, CommonParam.USER_PUBLISH_DISCUSS_SYMBOL);
                    if (!originSet.contains(id)){
                        originSet.add(id);
                        redisTemplate.opsForHash().put(userKye,CommonParam.USER_PUBLISH_DISCUSS_SYMBOL,originSet);
                    }
                }
            }else if (type == 2){
                //评论
                if (redisTemplate.hasKey(userKye)){
                    Set<Integer> originSet = (Set<Integer>)redisTemplate.opsForHash().get(userKye, CommonParam.USER_PUBLISH_COMMENT_SYMBOL);
                    if (!originSet.contains(id)){
                        originSet.add(id);
                        redisTemplate.opsForHash().put(userKye,CommonParam.USER_PUBLISH_COMMENT_SYMBOL,originSet);
                    }
                }
            }
        }catch (Exception e){
            throw new RuntimeException("点赞异常 以后做项目麻烦把注释 测试 完成后再写其他的  这里被自己的逻辑写晕了  不管了   不能给二级评论点赞  kafka无法通知点赞用户");
        }


        //利用kafka存储点赞消息  如果是取消点赞就不处理了
        if (!like){
            Event event = new Event();
            event.setTopic(CommonParam.KAFKA_TOPIC_LIKE).setUserId(currId).setEntityType(type).setEntityId(id).setEntityUserId(likeUserId);
            messageProducer.send(event);
        }

        return result;
    }
}
